package com.codejam2013.qualification;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;


public class B {

	static int nTest, N, M, count;
	static int[][] land, mark;
	
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(new File("B-in.txt"));
		PrintStream ps = new PrintStream(new File("B-out.txt"));
		System.setOut(ps);
		nTest = scanner.nextInt();
		for (int test=1;test<=nTest;++test) {
			N = scanner.nextInt();
			M = scanner.nextInt();
			land = new int[N+1][M+1];
			mark = new int[N+1][M+1];
			for (int i=1;i<=N;++i) {
				for (int j=1;j<=M;++j) {
					land[i][j] = scanner.nextInt();
					mark[i][j] = 0;
				}
			}
			System.out.print("Case #" + test + ": ");
			count = 0;
			boolean possible = true;
			for (int i=1;i<=N;++i) {
				for (int j=1;j<=M;++j) {
					if (mark[i][j] == 0) {
						if (dfsHori(i,j) || dfsVert(i,j)) {
							mark[i][j] = 1;
							count++;
//							System.out.println(i + " " + j + " " + count);
						}
						else {
							possible = false;
							break;
						}
					}
				}
				if (!possible) {
					break;
				}
			}
			if (!possible) {
				System.out.println("NO");
			}
			else {
				System.out.println("YES");
			}
		}

	}

	private static boolean dfsHori(int r, int c) {
		boolean hasHigher = false;
		for (int j=1;j<=M;++j) {
			if (j!=c) {
				if (land[r][j] > land[r][c]) {
					hasHigher = true;
					break;
				}
			}
		}
		if (hasHigher) {
			return false;
		}
		else {
			for (int j=1;j<=M;++j) {
				if (j!=c) {
					if (land[r][j] == land[r][c] && mark[r][j] == 0) {
						mark[r][j] = 1;
//						System.out.println(r + " " + j + " " + count);
						count++;
					}
				}
			}
			boolean possible = true;
			for (int j=1;j<=M;++j) {
				if (j!=c) {
					if (land[r][j] < land[r][c] && mark[r][j] == 0) {
						possible &= (dfsHori(r,j) || dfsVert(r,j));
					}
				}
			}
			return possible;
		}
	}

	private static boolean dfsVert(int r, int c) {
		boolean hasHigher = false;
		for (int i=1;i<=N;++i) {
			if (i!=r) {
				if (land[i][c] > land[r][c]) {
					hasHigher = true;
					break;
				}
			}
		}
		if (hasHigher) {
			return false;
		}
		else {
			for (int i=1;i<=N;++i) {
				if (i!=r) {
					if (land[i][c] == land[r][c] && mark[i][c] == 0) {
//						System.out.println(i + " " + c + " " + count);
						mark[i][c] = 1;
						count++;
					}
				}
			}
			boolean possible = true;
			for (int i=1;i<=N;++i) {
				if (i!=r) {
					if (land[i][c] < land[r][c] && mark[i][c] == 0) {
						possible &= (dfsHori(i,c) || dfsVert(i,c));
					}
				}
			}
			return possible;
		}
	}
	
}
